const homeApi = require('../../services/home');

// 获取应用实例
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Component({
  data: {
    tabsList: [
      { id: 1, title:'HTML5' },
      { id: 2, title:'CSS3' },
      { id: 3, title:'VUE' },
      { id: 4, title:'REACT' },
      { id: 5, title:'NODE' },
      { id: 6, title:'NUXT' },
      { id: 7, title:'微信小程序' },
      { id: 8, title:'UNIAPP' },
      { id: 9, title:'其他' },
    ],
    offsetTop: 84,
    homeActive: 0,
    isCircular: true,//默认衔接滑动
    interval: 3000,//默认自动切换时间间隔
    duration: 1000,//默认滑动动画时长
    imgUrls: [
      { url: 'https://ke-image.ljcdn.com/110000-inspection/pc1_x09FyssgN_1.jpg!m_fill,w_280,h_210,f_jpg?from=ke.com' },
      { url: 'https://ke-image.ljcdn.com/110000-inspection/pc1_D7hVOdrpE_1.jpg!m_fill,w_280,h_210,f_jpg?from=ke.com' },
      { url: 'https://ke-image.ljcdn.com/110000-inspection/pc1_tlE2WHNzS_1.jpg!m_fill,w_280,h_210,f_jpg?from=ke.com' }
    ],
    motto: 'Hello World',
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    paperdetails: [{

    }],
  },
  pageLifetimes: {
    show: function() {
      // 页面被展示
      if(typeof this.getTabBar==='function'&&this.getTabBar()){
        this.getTabBar().setData({
          selected: 0
        })
      }
      // this.getList();
    },
    hide: function() {
      // 页面被隐藏
    },
    resize: function(size) {
      // 页面尺寸变化
    }
  },
  methods: {
    compositionTap() {
      // wx.navigateTo({url: "pages/activity/composition/index"})
      wx.navigateTo({
        url: `/pages/activity/composition/index`
      })      
    },
    foodTap() {
      // wx.navigateTo({url: "pages/activity/composition/index"})
      wx.navigateTo({
        url: `/pages/activity/food/index`
      })      
    },
    async getList() {
      let param = '红烧鱼';
      let res = await homeApi.getFoodMenu(param);
      console.log("res", res)
    },
    onChange(event: { detail: { name: any; }; }) {
      wx.showToast({
        title: `切换到标签 ${event.detail.name}`,
        icon: 'none',
      });
    },
    onBeforeChange(event: { detail: { callback: any; title: any; }; }) {
      const { callback, title } = event.detail;
      callback(true)
      // wx.showModal({
      //   title: '异步切换',
      //   content: `确定要切换至 ${title} tab吗？`,
      //   success: (res) => {
      //     if (res.confirm) {
      //       callback(true)
      //     } else if (res.cancel) {
      //       callback(false)
      //     }
      //   },
      // })
    },
    // 事件处理函数
    bindViewTap() {
      wx.navigateTo({
        url: '../logs/logs',
      })
    },
    onChooseAvatar(e: any) {
      const { avatarUrl } = e.detail
      const { nickName } = this.data.userInfo
      this.setData({
        "userInfo.avatarUrl": avatarUrl,
        hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
      })
    },
    onInputChange(e: any) {
      const nickName = e.detail.value
      const { avatarUrl } = this.data.userInfo
      this.setData({
        "userInfo.nickName": nickName,
        hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
      })
    },
    getUserProfile() {
      // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
      wx.getUserProfile({
        desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log(res)
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    },
  },
})
